import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

Dio dio = new Dio();
/**
 * 主页切换+网络请求
 */
class Mainpage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "主页页签切换",
      home: MyDemo(),
    );
  }
}

class MyDemo extends StatefulWidget {
  MyDemo({Key? key, this.param}) : super(key: key);

  final String? param;

  @override
  _MyDemo createState() => _MyDemo();
}

class _MyDemo extends State<MyDemo> {
  int _current = 0;

  var bannerArr = [];


  getIndex() async {
    var response = await dio.get("https://www.wanandroid.com/banner/json");
    print(response.data["data"]);
    setState(() {
      bannerArr = response.data["data"];
    });
  }

  @override
  void initState() {
    super.initState();
    getIndex();
  }

  @override
  Widget build(BuildContext context) {
    final List<Widget> pages = [
      Center(
          child: ListView.builder(
              itemCount: bannerArr.length,
              itemBuilder: (BuildContext ctx, int i) {
                var banner = bannerArr[i];
                // return Text(banner['title']);
                return Container(
                  child: Row(
                    children: [
                      Image.network(banner['imagePath'],width: 200,height: 250,),
                      Text(banner['title']),
                    ],
                  ),
                );
              })),
      Center(child: Text('消息内容', style: TextStyle(fontSize: 24))),
      Center(child: Text('我的内容', style: TextStyle(fontSize: 24))),
    ];

    return Scaffold(
      appBar: AppBar(
        title: Text("首页"),
        centerTitle: true,
      ),
      drawer: Drawer(),
      body: pages[_current],
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _current,
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.home), label: "首页"),
          BottomNavigationBarItem(icon: Icon(Icons.message), label: "消息"),
          BottomNavigationBarItem(icon: Icon(Icons.person), label: "我的"),
        ],
        onTap: (index) {
          setState(() {
            _current = index;
          });
        },
      ),
    );
  }
}
